from db.models.conversation_history import ConversationHistory
from db.repository.base import BaseRepository
from sqlalchemy.orm import Session
from typing import List


class ConversationHistoryRepository(BaseRepository[ConversationHistory]):
    def __init__(self, db: Session):
        super().__init__(db, ConversationHistory)

    def get_by_session_id(self, session_id: str) -> List[ConversationHistory]:
        return (
            self.db.query(self.model)
            .filter(self.model.session_id == session_id)
            .order_by(self.model.created_at.asc())
            .all()
        )

    def get_by_user_id(self, user_id: str) -> List[ConversationHistory]:
        return (
            self.db.query(self.model)
            .filter(self.model.user_id == user_id)
            .order_by(self.model.created_at.desc())
            .all()
        )

    def delete_by_session_id(self, session_id: str) -> int:
        return (
            self.db.query(self.model)
            .filter(self.model.session_id == session_id)
            .delete(synchronize_session=False)
        )
